﻿using System;
using Microsoft.Extensions.DependencyInjection;
using Avalonia;
using Avalonia.Controls;

using Klear.Library.Services;
using Klear.Library.ViewModels;
using Klear.Services;



namespace Klear;

public class ServiceLocator
{
    private readonly IServiceProvider _serviceProvider;

    private static ServiceLocator _current;

    public static ServiceLocator Current {
        get
        {
            if (_current is not null) {
                return _current;
            }

            if (Application.Current.TryGetResource(nameof(ServiceLocator),
                    out var resource) &&
                resource is ServiceLocator serviceLocator) {
                return _current = serviceLocator;
            }

            throw new Exception("我解决不了这种错误");
        }
    }
    
    
    public BudgetViewModel BudgetViewModel =>
        _serviceProvider.GetRequiredService<BudgetViewModel>();
    public IncomeExpensesViewModel IncomeExpensesViewModel =>
        _serviceProvider.GetRequiredService<IncomeExpensesViewModel>();
    
    public InitializationViewModel InitializationViewModel =>
        _serviceProvider.GetRequiredService<InitializationViewModel>();
    
    public MainViewModel MainViewModel => 
        _serviceProvider.GetRequiredService<MainViewModel>();
    
    public MainWindowViewModel MainWindowViewModel =>
        _serviceProvider.GetRequiredService<MainWindowViewModel>();
    
    public CategoryViewModel CategoryViewModel =>
        _serviceProvider.GetRequiredService<CategoryViewModel>();
    
    public GlobalSearchViewModel GlobalSearchViewModel =>
        _serviceProvider.GetRequiredService<GlobalSearchViewModel>();
    
    public VisualizationViewModel VisualizationViewModel =>
        _serviceProvider.GetRequiredService<VisualizationViewModel>();
    
    public BudgetListViewModel BudgetListViewModel =>
        _serviceProvider.GetRequiredService<BudgetListViewModel>();
    
    
    public ServiceLocator()
    {
        var serviceCollection = new ServiceCollection();
        serviceCollection.AddSingleton<SharedDataService>();
        serviceCollection.AddSingleton<IOcrService, OcrService>();
        serviceCollection.AddSingleton<ChineseAlmanacService>();
        serviceCollection.AddSingleton<InterfaceService>();
        
        serviceCollection.AddSingleton<IDatabaseService,DatabaseService>();
        serviceCollection.AddSingleton<BudgetViewModel>();

        serviceCollection.AddSingleton<IDataStorageTemplate, DataBaseVersionStorage>();
        serviceCollection.AddSingleton<IBudgetService, BudgetService>();
        serviceCollection.AddSingleton<IIncomeAndExpensesService, IncomeAndExpensesService>();
        serviceCollection.AddSingleton<ICategoryService, CategoryService>();

        serviceCollection.AddSingleton<IncomeExpensesViewModel>();
        serviceCollection.AddSingleton<IRootNavigationService, RootNavigationService>();
        serviceCollection.AddSingleton<InitializationViewModel>();
        serviceCollection.AddSingleton<MainViewModel>();
        serviceCollection.AddSingleton<MainWindowViewModel>();
        serviceCollection.AddSingleton<IMenuNavigationService, MenuNavigationService>();
        serviceCollection.AddSingleton<BudgetViewModel>();
        serviceCollection.AddSingleton<CategoryViewModel>();
        serviceCollection.AddSingleton<GlobalSearchViewModel>();
        serviceCollection.AddSingleton<VisualizationViewModel>();
        serviceCollection.AddSingleton<IVisualizationService, VisualizationService>();
        serviceCollection.AddSingleton<IRemindStorage, RemindStorage>();
        serviceCollection.AddSingleton<BudgetListViewModel>();
        serviceCollection.AddSingleton<IContentNavigationService, ContentNavigationService>();
        serviceCollection.AddSingleton<IGlobalSearchService, GlobalSearchService>();
        
        _serviceProvider = serviceCollection.BuildServiceProvider();
    }

}